﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo;
using StockTrader.BusinessService.DataContract.HnxInfo;
using MySql.Data.MySqlClient;
using System.Data;
using StockTrader.DAL.MySQLHelper;


namespace StockTrader.DAL.MySQL.HnxInfo
{
    public class HnxDeal : IHnxDeal
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters

        private const string PARM_DEAL_ID = "@DEAL_ID";
        private const string PARM_ORDER_NO = "@ORDER_NO";
        private const string PARM_CO_ORDER_NO = "@COORDER_NO";
        private const string PARM_ORDER_CONFIRM_NO = "@ORDER_CONFIRM_NO";
        private const string PARM_DEAL_DATE = "@DEAL_DATE";
        private const string PARM_DEAL_TIME = "@DEAL_TIME";
        private const string PARM_DEAL_QTTY = "@DEAL_QTTY";
        private const string PARM_DEAL_PRICE = "@DEAL_PRICE";
        private const string PARM_CO_ACCOUNT_NO = "@COACCOUNT_NO";
        private const string PARM_ACCOUNT_NO = "@ACCOUNT_NO";
        private const string PARM_CO_BROKER_ID = "@COBROKER_ID";
        private const string PARM_TIMESTAMP = "@TIMESTAMP";

        #endregion

        #region SQL

        private const string INSERT_HNX_NEWDEAL = "INSERT INTO HNX_DEALS(DEAL_ID, ORDER_NO, ORDER_CONFIRM_NO, COORDER_NO, DEAL_DATE, DEAL_TIME, DEAL_PRICE, DEAL_QTTY, ACCOUNT_NO, COACCOUNT_NO, COBROKER_ID, TIMESTAMP) VALUES (@DEAL_ID, @ORDER_NO, @ORDER_CONFIRM_NO, @COORDER_NO, @DEAL_DATE, @DEAL_TIME, @DEAL_PRICE, @DEAL_QTTY, @ACCOUNT_NO, @COACCOUNT_NO, @COBROKER_ID, @TIMESTAMP) ON DUPLICATE KEY UPDATE DEAL_ID=@DEAL_ID, ORDER_NO=@ORDER_NO, ORDER_CONFIRM_NO=@ORDER_CONFIRM_NO, COORDER_NO=@COORDER_NO, DEAL_DATE=@DEAL_DATE, DEAL_TIME=@DEAL_TIME, DEAL_PRICE=@DEAL_PRICE, DEAL_QTTY=@DEAL_QTTY, ACCOUNT_NO=@ACCOUNT_NO, COACCOUNT_NO=@COACCOUNT_NO, COBROKER_ID=@COBROKER_ID, TIMESTAMP=@TIMESTAMP";

        private const string GET_HNX_ALLDEALS = "SELECT * FROM HNX_DEALS WHERE DEAL_DATE=CURRENT_DATE";

        private const string GET_HNX_LASTDEALS = "SELECT * FROM HNX_DEALS WHERE TIMESTAMP>@TIMESTAMP";
        #endregion

        #region IHnxDeal Members


        public List<HnxDealData> getHnxNewDeals(decimal timeStamp)
        {
            throw new NotImplementedException();
        }

        public List<HnxDealData> getHnxAllDeals()
        {
            try
            {
                List<HnxDealData> list = new List<HnxDealData>();

                MySqlDataReader rdr = MyHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_ALLDEALS);

                while (rdr.Read())
                {
                    list.Add(new HnxDealData(
                                Convert.ToDecimal(rdr["DEAL_ID"]),
                                rdr["ORDER_NO"].ToString(),
                                rdr["ORDER_CONFIRM_NO"].ToString(),
                                rdr["COORDER_NO"].ToString(),
                                Convert.ToDateTime(rdr["DEAL_DATE"]),
                                rdr["DEAL_TIME"].ToString(),
                                Convert.ToDecimal(rdr["DEAL_PRICE"]),
                                Convert.ToDecimal(rdr["DEAL_QTTY"]),
                                rdr["ACCOUNT_NO"].ToString(),
                                rdr["COACCOUNT_NO"].ToString(),
                                Convert.ToInt32(rdr["COBROKER_ID"]),
                                Convert.ToInt64(rdr["TIMESTAMP"])));
                }

                rdr.Close();
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        public List<HnxDealData> countHnxDeals()
        {
            throw new NotImplementedException();
        }

        public void deleteHnxDeals()
        {
            throw new NotImplementedException();
        }

        public void insertHnxNewDeal(HnxDealData deal)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                    new MySqlParameter( PARM_DEAL_ID,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_ORDER_NO,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_ORDER_CONFIRM_NO,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_CO_ORDER_NO,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_DEAL_DATE,MySqlDbType.DateTime),
                    new MySqlParameter( PARM_DEAL_TIME,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_DEAL_PRICE,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_DEAL_QTTY,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_ACCOUNT_NO,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_CO_ACCOUNT_NO,MySqlDbType.VarChar),
                    new MySqlParameter( PARM_CO_BROKER_ID,MySqlDbType.Decimal),
                    new MySqlParameter( PARM_TIMESTAMP,MySqlDbType.Int64)
                };

                parms[0].Value = deal.DealId;
                parms[1].Value = deal.OrderNo;
                parms[2].Value = deal.OrderConfirmNo;
                parms[3].Value = deal.CoOrderNo;
                parms[4].Value = deal.DealDate;
                parms[5].Value = deal.DealTime;
                parms[6].Value = deal.DealPrice;
                parms[7].Value = deal.DealQtty;
                parms[8].Value = deal.AccountNo;
                parms[9].Value = deal.CoAccountNo;
                parms[10].Value = deal.CoBrokerId;
                parms[11].Value = deal.TimeStamp;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_HNX_NEWDEAL, parms);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<HnxDealData> getHnxLastDeals(long timestamp)
        {
            try
            {
                MySqlParameter parm1 = new MySqlParameter("@TIMESTAMP", MySqlDbType.Int64);
                parm1.Value = timestamp;

                List<HnxDealData> list = new List<HnxDealData>();

                MySqlDataReader rdr = MyHelper.ExecuteReader(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_LASTDEALS, parm1);

                while (rdr.Read())
                {
                    list.Add(new HnxDealData(
                                Convert.ToDecimal(rdr["DEAL_ID"]),
                                rdr["ORDER_NO"].ToString(),
                                rdr["ORDER_CONFIRM_NO"].ToString(),
                                rdr["COORDER_NO"].ToString(),
                                Convert.ToDateTime(rdr["DEAL_DATE"]),
                                rdr["DEAL_TIME"].ToString(),
                                Convert.ToDecimal(rdr["DEAL_PRICE"]),
                                Convert.ToDecimal(rdr["DEAL_QTTY"]),
                                rdr["ACCOUNT_NO"].ToString(),
                                rdr["COACCOUNT_NO"].ToString(),
                                Convert.ToInt32(rdr["COBROKER_ID"]),
                                Convert.ToInt64(rdr["TIMESTAMP"])));
                }

                rdr.Close();
                return list;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        #endregion
    }
}
